package cn.han.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Scanner;

public class Test5 {
    public static void main(String[] args) throws Exception {
        System.out.println("请输入用户名：");
        String s = new Scanner(System.in).nextLine();
        System.out.println("请输入密码：");
        String s1 = new Scanner(System.in).nextLine();
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/cgb2018";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from users where uname = '"+s+"'");

        while (true) {
            if (resultSet.next()) {
                String a = resultSet.getString(2);
                String b = resultSet.getString(3);
                if (a.equals(s)) {
                    while (true) {
                        if (b.equals(s1)) {
                            System.out.println("登陆成功");
                            break;
                        } else {
                            System.out.println("密码错误请重新输入");
                            s1 = new Scanner(System.in).nextLine();
                        }
                    }
                    break;
                }

            } else {
                System.out.println("登陆失败，用户名不存在！");
                break;

            }

        }
        resultSet.close();
        statement.close();
        connection.close();


    }
}
